package com.yu.footballpromotionliveadmin.service.impl;

import com.yu.footballpromotionliveadmin.dao.EventRecordsDao;
import com.yu.footballpromotionliveadmin.dto.DTO;
import com.yu.footballpromotionliveadmin.dto.MessageEnum;
import com.yu.footballpromotionliveadmin.page.PageInfo;
import com.yu.footballpromotionliveadmin.pojo.EventRecords;
import com.yu.footballpromotionliveadmin.service.EventRecordsService;
import com.yu.footballpromotionliveadmin.vo.EventRecordsView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Optional;

@Service
public class EventRecordsServiceImpl implements EventRecordsService {
    
    @Autowired
    EventRecordsDao eventRecordsDao;


    @Override
    public DTO<EventRecords> getAllEventRecordss() throws Exception {
        List<EventRecords> activityTypes = eventRecordsDao.getAllCompetitionRecors();
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum = Optional.ofNullable(activityTypes).isPresent() ? MessageEnum.GETADMINSUCCESS : MessageEnum.GETADMINFAIL;

        return new DTO<EventRecords>(messageEnum.getCode(),messageEnum.getMessage(),activityTypes);
    }

    @Override
    public DTO<EventRecords> getEventRecordsById(String erno) throws Exception {

        EventRecords activityType = eventRecordsDao.getCompetitionRecorByUno(erno);

        MessageEnum messageEnum = Optional.ofNullable(activityType).isPresent() ? MessageEnum.GETADMINSUCCESS : MessageEnum.GETADMINFAIL;
        return new DTO<EventRecords>(messageEnum.getCode(),messageEnum.getMessage(),activityType);
    }

    @Override
    public DTO<EventRecords> deleteEventRecords(String erno) throws Exception {
        Integer integer = eventRecordsDao.deleteCompetitionRecor(erno);

        MessageEnum messageEnum = integer > 0 ? MessageEnum.DELETEADMINSUCCESS : MessageEnum.DELETEADMINFAIL;
        return new DTO<EventRecords>(messageEnum.getCode(),messageEnum.getMessage());
    }

    @Override
    public DTO<EventRecords> updateEventRecords(EventRecordsView activityTypeView) throws Exception {
        EventRecords eventRecords = dateFormat(activityTypeView);

        Integer integer = eventRecordsDao.updateCompetitionRecor(eventRecords);

        MessageEnum messageEnum = integer > 0 ? MessageEnum.UPDATEADMINSUCCESS : MessageEnum.UPDATEADMINFAIL;
        return new DTO<EventRecords>(messageEnum.getCode(),messageEnum.getMessage());
    }

    @Override
    public DTO<EventRecords> addEventRecords(EventRecordsView activityTypeView) throws Exception {
        EventRecords eventRecords = dateFormat(activityTypeView);
        Integer integer = eventRecordsDao.addCompetitionRecorByUno(eventRecords);
        MessageEnum messageEnum = integer > 0 ? MessageEnum.ADDADMINSUCCESS : MessageEnum.ADDADMINFAIL;
        return new DTO<EventRecords>(messageEnum.getCode(),messageEnum.getMessage(),eventRecords);
    }

    @Override
    public DTO<EventRecords> getEventRecordssByPage(PageInfo pageInfo) throws Exception {
        List<EventRecords> activityTypesByPage = eventRecordsDao.getCompetitionRecorsByPage(pageInfo);
        MessageEnum messageEnum = Optional.ofNullable(activityTypesByPage).isPresent() ? MessageEnum.GETADMINSUCCESS : MessageEnum.GETADMINFAIL;

        return new DTO<EventRecords>(messageEnum.getCode(),messageEnum.getMessage(),activityTypesByPage);
    }

    @Override
    public DTO<EventRecords> getEventRecordssWithOrders(String erno) throws Exception {
        return null;
    }

    @Override
    public DTO<EventRecords> activate(String erno) throws Exception {
        return null;
    }

    @Override
    public DTO<EventRecords> lettory(String erno, String giftName) throws Exception {
        return null;
    }

    public EventRecords dateFormat(EventRecordsView eventRecordsView) throws ParseException {
        EventRecords eventRecords = new EventRecords();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        eventRecords.setErno(eventRecordsView.getErno());
        eventRecords.setEname(eventRecordsView.getEname());
        eventRecords.setEtime(eventRecordsView.getEtime());
        eventRecords.setStime(eventRecordsView.getStime());
        eventRecords.setMtno(eventRecordsView.getMtno());
        eventRecords.setNotnum(eventRecordsView.getNotnum());
        eventRecords.setYear(sdf.parse(eventRecordsView.getYear()));
        eventRecords.setVenue(eventRecordsView.getVenue());
        return eventRecords;
    }
}
